﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;

namespace KZ.Express.Order.Order
{
    public partial class SurchargeView : FormBase
    {
        #region Private Variables

        private new OrderSurchargePresenter _presenter;

        #endregion Private Variables

        #region Private Methods

        private void SurchargeView_Load(object sender, EventArgs e)
        {
            dtpCFrom.Value = DateTime.Now.GetFirstDayOfMonth();
            dtpCTo.Value = DateTime.Now.GetLastDayOfMonth();
            LoadCarrier();
            LoadCustomer();
        }

        private void txtRate_Leave(object sender, EventArgs e)
        {
            int decimalDigit = Utility.Info.NumberFormat.CurrencyDecimalDigits;
            Utility.Info.NumberFormat.CurrencyDecimalDigits = 0;
            try
            {
                decimal? value = Utility.DecimalNParse(txtCRate.Text);
                txtCRate.Text = Utility.CurrencyParse(value, Utility.Info);
            }
            finally
            {
                Utility.Info.NumberFormat.CurrencyDecimalDigits = decimalDigit;
            }
        }

        private void txtKRate_Leave(object sender, EventArgs e)
        {
            int decimalDigit = Utility.Info.NumberFormat.CurrencyDecimalDigits;
            Utility.Info.NumberFormat.CurrencyDecimalDigits = 0;
            try
            {
                decimal? value = Utility.DecimalNParse(txtKRate.Text);
                txtKRate.Text = Utility.CurrencyParse(value, Utility.Info);
            }
            finally
            {
                Utility.Info.NumberFormat.CurrencyDecimalDigits = decimalDigit;
            }
        }

        private void txtKCost_Leave(object sender, EventArgs e)
        {
            decimal? value = Utility.DecimalNParse(txtKCost.Text);
            txtKCost.Text = Utility.CurrencyParse(value, Utility.Info);
        }

        private void txtKVNDListOptional_Leave(object sender, EventArgs e)
        {
            int decimalDigit = Utility.Info.NumberFormat.CurrencyDecimalDigits;
            Utility.Info.NumberFormat.CurrencyDecimalDigits = 0;
            try
            {
                decimal? value = Utility.DecimalNParse(txtKVNDListOptional.Text);
                txtKVNDListOptional.Text = Utility.CurrencyParse(value, Utility.Info);
            }
            finally
            {
                Utility.Info.NumberFormat.CurrencyDecimalDigits = decimalDigit;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                decimal? orivat;
                if (txtCVAT.Text == "0")
                    orivat = 0;
                else
                    orivat = Utility.DecimalNParse(txtCVAT.Text);

                decimal? vat;
                if (txtCVAT2.Text == "0")
                    vat = 0;
                else
                    vat = Utility.DecimalNParse(txtCVAT2.Text);

                decimal? orifuel;
                if (txtCFuel.Text == "0")
                    orifuel = 0;
                else
                    orifuel = Utility.DecimalNParse(txtCFuel.Text);

                decimal? fuel;
                if (txtCFuel2.Text == "0")
                    fuel = 0;
                else
                    fuel = Utility.DecimalNParse(txtCFuel2.Text);

                decimal? rate;
                if (txtCRate.Text == "0")
                    rate = 0;
                else
                    rate = Utility.DecimalNParse(txtCRate.Text);

                _presenter.UpdateSurcharge(Utility.IntParse(cmbCarrierID.SelectedValue), dtpCFrom.Value, dtpCTo.Value,
                    orivat, vat, orifuel, fuel, rate);
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void btnKUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                decimal? orivat;
                if (txtKVAT.Text == "0")
                    orivat = 0;
                else
                    orivat = Utility.DecimalNParse(txtKVAT.Text);

                decimal? vat;
                if (txtKVAT2.Text == "0")
                    vat = 0;
                else
                    vat = Utility.DecimalNParse(txtKVAT2.Text);

                decimal? orifuel;
                if (txtKFuel.Text == "0")
                    orifuel = 0;
                else
                    orifuel = Utility.DecimalNParse(txtKFuel.Text);

                decimal? fuel;
                if (txtKFuel2.Text == "0")
                    fuel = 0;
                else
                    fuel = Utility.DecimalNParse(txtKFuel2.Text);

                decimal? rate;
                if (txtKRate.Text == "0")
                    rate = 0;
                else
                    rate = Utility.DecimalNParse(txtKRate.Text);

                decimal? cost;
                if (txtKCost.Text == "0")
                    cost = 0;
                else
                    cost = Utility.DecimalNParse(txtKCost.Text);

                decimal? vndListPercent;
                if (txtKVNDListPercent.Text == "0")
                    vndListPercent = 0;
                else
                    vndListPercent = Utility.DecimalNParse(txtKVNDListPercent.Text);

                decimal? vndListOptional;
                if (txtKVNDListOptional.Text == "0")
                    vndListOptional = 0;
                else
                    vndListOptional = Utility.DecimalNParse(txtKVNDListOptional.Text);

                _presenter.UpdateSurcharge(Utility.IntParse(cmbCustomerID.SelectedValue), dtpKFrom.Value, dtpKTo.Value,
                    orivat, vat, orifuel, fuel, rate, cost, vndListOptional, txtDescription.Text);
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void btnKCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoadCarrier()
        {
            try
            {
                Global.LoadComboBox(cmbCarrierID, base._presenter.LoadForCMB<CarrierENT>(null));
                cmbCarrierID.SelectedValue = this.CarrierID;
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadCustomer()
        {
            try
            {
                SearchCriteria search = new SearchCriteria();
                search.Add("IsCustomer", 1);
                Global.LoadComboBox(cmbCustomerID, base._presenter.LoadForCMB<CustomerENT>(search));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion Private Methods

        #region Constructors

        public SurchargeView()
        {
            InitializeComponent();

            cmbCustomerID.DisplayColumns.Add("PartnerCode", "Mã KH");
            cmbCustomerID.DisplayColumns.Add("PartnerName", "Tên KH");

            this.TopLevel = true;
            base._presenter = _presenter = new OrderSurchargePresenter(this);
        }

        #endregion Constructors

        #region Public Properties

        public int CarrierID { get; set; }

        #endregion Public Properties
    }
}